package com.gitee.zycra.union.api.web.controller;

import com.gitee.zycra.jdbc.model.PageResult;
import com.gitee.zycra.union.api.common.bo.CommonResult;
import com.gitee.zycra.union.api.common.bo.PageQuery;
import com.gitee.zycra.union.api.common.param.AddUserParam;
import com.gitee.zycra.union.api.common.param.PageQueryUserParam;
import com.gitee.zycra.union.api.common.param.UpdatePasswordParam;
import com.gitee.zycra.union.api.common.param.UserLoginParam;
import com.gitee.zycra.union.api.common.vo.UserVO;
import com.gitee.zycra.union.api.common.vo.ProjectGroupVO;
import com.gitee.zycra.union.api.common.vo.UserLoginVO;
import com.gitee.zycra.union.api.service.ApiUserService;
import com.gitee.zycra.union.api.web.annotation.AuthCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zycra
 */
@RestController
@RequestMapping("/user")
public class ApiUserController {

    @Autowired
    private ApiUserService apiUserService;

    /**
     * 添加
     *
     * @param addUserParam 用户对象
     * @return 添加结果
     */
    @AuthCheck({"0"})
    @PostMapping("/addUser")
    public CommonResult<String> addUser(@RequestBody @Validated AddUserParam addUserParam) {
        return apiUserService.insertApiUser(addUserParam);
    }

    /**
     * 禁用/启用
     *
     * @param id      用户ID
     * @param enabled 是否启用
     * @return 影响条数
     */
    @AuthCheck({"0"})
    @PostMapping("/switchEnabled")
    public CommonResult<String> switchEnabled(@RequestParam("id") Long id, @RequestParam("enabled") Boolean enabled) {
        return apiUserService.switchEnabled(id, enabled);
    }

    /**
     * 更新密码
     *
     * @param updatePasswordParam 修改密码对象
     * @return 更新结果
     */
    @AuthCheck
    @PostMapping("/updatePassword")
    public CommonResult<String> updatePassword(@RequestBody @Validated UpdatePasswordParam updatePasswordParam) {
        return apiUserService.updatePassword(updatePasswordParam);
    }

    /**
     * 分页查询
     *
     * @param pageQuery 查询参数
     * @return 查询结果
     */
    @AuthCheck({"0"})
    @PostMapping("/pageList")
    public CommonResult<PageResult<UserVO>> pageList(@RequestBody @Validated PageQuery<PageQueryUserParam> pageQuery) {
        PageResult<UserVO> pageResult = apiUserService.selectApiUserForPage(pageQuery);
        return CommonResult.success(pageResult);
    }

    /**
     * 登录
     *
     * @param userLoginParam 登录对象
     * @return token
     */
    @PostMapping("/login")
    public CommonResult<UserLoginVO> login(@RequestBody @Validated UserLoginParam userLoginParam) {
        return apiUserService.login(userLoginParam);
    }

    /**
     * 获取验证码
     *
     * @return token
     */
    @GetMapping("/getCaptcha")
    public CommonResult<UserLoginVO> getCaptcha() {
        return apiUserService.getCaptcha();
    }

    /**
     * 查询当前用户的项目组
     *
     * @return 查询结果
     */
    @AuthCheck
    @GetMapping("/userGroupList")
    public CommonResult<List<ProjectGroupVO>> userGroupList() {
        return apiUserService.userGroupList();
    }
}
